#include "stdafx.h"
#include "StringUtil.h"

/*
*	Convert Ansi(string) <-> UTF16(wstring) <-> UTF8(string)
*	Return : Success=Size of Result string, Fail=0 (kimsk. 2005.12.1)
*/
int CStringUtil::AnsiToUTF16(const string& strAnsi, wstring& strUnicode)
{
	int len = ::MultiByteToWideChar(CP_ACP, 0, strAnsi.c_str(), -1, NULL, 0);
	if(len == 0)	return len;
	wchar_t* wbuf = new wchar_t[len];
	len = ::MultiByteToWideChar(CP_ACP, 0, strAnsi.c_str(), -1, wbuf, len);
	if(len != 0)	strUnicode = wbuf; 
	delete[] wbuf;
	return len;
}

int CStringUtil::UTF16ToUTF8(const wstring& strUnicode, string& strUtf8)
{
	int len = ::WideCharToMultiByte(CP_UTF8, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);
	if(len == 0)	return len;
	char* buf = new char[len];
	len = ::WideCharToMultiByte(CP_UTF8, 0, strUnicode.c_str(), -1, buf, len, NULL, NULL);
	if(len != 0)	strUtf8 = buf;
	delete[] buf;
	return len;
}

int CStringUtil::AnsiToUTF8(const string& strAnsi, string& strUtf8)
{
	wstring strUnicode;
	if(AnsiToUTF16(strAnsi, strUnicode) != 0)
		return UTF16ToUTF8(strUnicode, strUtf8);
	return 0;
}

int CStringUtil::UTF8ToUTF16(const string& strUtf8, wstring& strUnicode)
{   
	int len = ::MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, NULL, 0);
	if(len == 0)	return len;
	wchar_t* wbuf = new wchar_t[len];
	len = ::MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, wbuf, len);
	if(len != 0)	strUnicode = wbuf;
	delete[] wbuf;
	return len;
}

int CStringUtil::UTF16ToAnsi(const wstring& strUnicode, string& strAnsi)
{
	int len = ::WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);
	if(len == 0)	return len;
	char* buf = new char[len];
	len = ::WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, buf, len, NULL, NULL);
	if(len != 0)	strAnsi = buf;
	delete[] buf;
	return len;
}

int CStringUtil::UTF8ToAnsi(const string& strUtf8, string& strAnsi)
{
	wstring strUnicode;
	if(UTF8ToUTF16(strUtf8, strUnicode) != 0)
		return UTF16ToAnsi(strUnicode, strAnsi);
	return 0;
}

///////////////////////////////////////////////////////////
wstring CStringUtil::AnsiToUTF16(const string& strAnsi)
{
	wstring strUnicode = L"";

	AnsiToUTF16( strAnsi, strUnicode );

	return strUnicode;
}

wstring CStringUtil::UTF8ToUTF16(const string& strUtf8)
{   
	wstring strUnicode = L"";

	UTF8ToUTF16( strUtf8, strUnicode );

	return strUnicode;
}

string CStringUtil::UTF16ToUTF8(const wstring& strUnicode)
{
	string strUtf8 = "";

	UTF16ToUTF8( strUnicode, strUtf8 );

	return strUtf8;
}

string CStringUtil::AnsiToUTF8(const string& strAnsi)
{
	string strUtf8 = "";

	AnsiToUTF8( strAnsi, strUtf8 );

	return strUtf8;
}

string CStringUtil::UTF16ToAnsi(const wstring& strUnicode)
{
	string strAnsi = "";

	UTF16ToAnsi( strUnicode, strAnsi );

	return strAnsi;
}

string CStringUtil::UTF8ToAnsi(const string& strUtf8)
{
	string strAnsi = "";

	UTF8ToAnsi( strUtf8, strAnsi);

	return strAnsi;
}